package com.moli.lumpSugar.common.aspct;//package com.moli.lumpSugar.common.aspct;
//
//import cn.hutool.core.util.StrUtil;
//
//import com.moli.lumpSugar.common.annotation.FrequencyControl;
//import com.moli.lumpSugar.common.constant.FrequencyControlConstant;
//import com.moli.lumpSugar.common.domain.dto.FrequencyControlDTO;
//import com.moli.lumpSugar.common.domain.dto.TokenBucketDTO;
//import com.moli.lumpSugar.common.utils.RequestHolder;
//import com.moli.lumpSugar.common.utils.SpElUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.stereotype.Component;
//
//import java.lang.reflect.Method;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
///**
// * Description: 频控实现
// * Author: zhangjt
// * Date: 2024-11-12
// */
//@Slf4j
//@Aspect
//@Component
//public class FrequencyControlAspect {
//
//    @Around("@annotation(com.moli.lumpSugar.common.annotation.FrequencyControl)" +
//            "||@annotation(com.moli.lumpSugar.common.annotation.FrequencyControlContainer)")
//    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
//        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
//        FrequencyControl[] annotationsByType = method.getAnnotationsByType(FrequencyControl.class);
//        Map<String, FrequencyControl> keyMap = new HashMap<>();
//        for (int i = 0; i < annotationsByType.length; i++) {
//            FrequencyControl frequencyControl = annotationsByType[i];
//            String prefix = StrUtil.isBlank(frequencyControl.prefixKey()) ? SpElUtils.getMethodKey(method) + ":index:" + i : frequencyControl.prefixKey();//默认方法限定名+注解排名（可能多个）
//            String key = "";
//            switch (frequencyControl.target()) {
//                case EL:
//                    key = SpElUtils.parseSpEl(method, joinPoint.getArgs(), frequencyControl.spEl());
//                    break;
//                case IP:
//                    key = RequestHolder.get().getIp();
//                    break;
//                case UID:
//                    key = RequestHolder.get().getUid().toString();
//            }
//            keyMap.put(prefix + ":" + key, frequencyControl);
//        }
//        List<String> ControlTypeList = keyMap.entrySet().stream().map(entrySet -> entrySet.getValue().ControlType()).collect(Collectors.toList());
//        if (ControlTypeList.isEmpty()) {
//            ControlTypeList.add(FrequencyControlConstant.TOTAL_COUNT_WITH_IN_FIX_TIME);
//        }
//        if (ControlTypeList.stream().distinct().count() > 1) {
//            log.warn("FrequencyControl注解参数ControlType必须一致，请检查");
//            return joinPoint.proceed();
//        }
//        // 将注解的参数转换为编程式调用需要的参数
//        List<FrequencyControlDTO> frequencyControlDTOS =
//                keyMap.entrySet().stream()
//                        .map(entrySet -> buildFrequencyControlDTO(entrySet.getKey(), entrySet.getValue())).collect(Collectors.toList());
//        // 调用编程式注解
//        return FrequencyControlUtil.executeWithFrequencyControlList(ControlTypeList.get(0), frequencyControlDTOS, joinPoint::proceed);
//    }
//
//    /**
//     * 将注解参数转换为编程式调用所需要的参数
//     *
//     * @param key              频率控制Key
//     * @param frequencyControl 注解
//     * @return 编程式调用所需要的参数-FrequencyControlDTO
//     */
//    private FrequencyControlDTO buildFrequencyControlDTO(String key, FrequencyControl frequencyControl) {
//        String controlType = frequencyControl.ControlType();
//        if (StrUtil.isBlank(controlType)){
//            controlType = FrequencyControlConstant.TOTAL_COUNT_WITH_IN_FIX_TIME;
//        }
//        switch(controlType) {
//            case FrequencyControlConstant.TOTAL_COUNT_WITH_IN_FIX_TIME:
//                FrequencyControlDTO frequencyControlDTO = new FrequencyControlDTO();
//                frequencyControlDTO.setCount(frequencyControl.count());
//                frequencyControlDTO.setTime(frequencyControl.time());
//                frequencyControlDTO.setUnit(frequencyControl.unit());
//                frequencyControlDTO.setKey(key);
//                return frequencyControlDTO;
//            case FrequencyControlConstant.TOKEN_BUCKET:
//                TokenBucketDTO tokenBucketDTO = new TokenBucketDTO(frequencyControl.count(), frequencyControl.refillRate());
//                tokenBucketDTO.setKey(key);
//                return tokenBucketDTO;
//            default:
//                return null;
//        }
//    }
//}
